﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace UpdaterGenerator
{
    public partial class formGenerateUpdate : Form
    {
        public formGenerateUpdate()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            if (DialogResult.OK == fbd.ShowDialog())
            {
                txtPath.Text = fbd.SelectedPath;
                btnScanSave.Enabled = true;
            }
        }

        Common.SettingsManager sm = new Common.SettingsManager();

        private void btnScanSave_Click(object sender, EventArgs e)
        {
            sm = new Common.SettingsManager();
            List<string> files = Directory.GetFiles(txtPath.Text, "*.dll", SearchOption.AllDirectories).ToList();
            files.AddRange(Directory.GetFiles(txtPath.Text, "*.exe", SearchOption.AllDirectories));

            files.RemoveAll(x => !x.Contains("Release"));

            if (sm.SettingsExists(false, this.GetType().FullName))
                sm.LoadSettings(false, this.GetType().FullName);

            foreach (string file in files)
            {
                try
                {
                    Assembly assembly = Assembly.LoadFile(file);
                    string key = string.Format("{0}-{1}", Common.AssemblyHelper.GetName(assembly), Common.AssemblyHelper.GetArchitecture(assembly).ToString());
                    sm.Add(key);
                    sm[key] = Common.AssemblyHelper.GetVersion(assembly).ToString();
                    key = string.Format("{0}-Download", key);
                    sm.Add(key);
                    sm[key] = "http://";
                }
                catch { }
            }

            sm.SaveSettings(true, this.GetType().FullName);
        }
    }
}
